/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.service;

import com.jmooc.dao.UserDao;
import com.jmooc.entity.User;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Group
 */
@Stateless
public class UserService implements Serializable {
     
    @EJB
    private UserDao userDao;
    
    public void create(User user) {
        user.setPassword(this.hashPassword(user.getPassword()));
        userDao.create(user);
    }
    
    public void update(User user) {
        if (!user.getPassword().startsWith("HASHED"))
            user.setPassword(this.hashPassword(user.getPassword()));
        
        userDao.update(user);
    }
    
    public User getById(Long id) {
        return userDao.getById(id);
    }
    
    public boolean canLogin(String login, String password) {
        try {
            User user = userDao.getByLogin(login);
            System.out.println(user);
            System.out.println(password);
            
            if (user.getPassword().equals(this.hashPassword(password)))
                return true;
        } catch (Exception e) {
        }
                
        
        return false;
    }
       
    public User getByEmail(String login) {
        User user = userDao.getByLogin(login);
                
        return user;
    }
    
    private String hashPassword(String password) {
        String hash = "";
	MessageDigest md = null;
        
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
        }
	
        if (md != null) {
            byte[] bArray = md.digest(("@@987" + password + "S3CUR1TY").getBytes());
            
            for (int i = 0; i < bArray.length; i++) {
                if ((0xff & bArray[i]) < 0x10) {
                    hash = hash + "0" + Integer.toHexString((0xFF & bArray[i]));
                } else {
                    hash = hash + Integer.toHexString(0xFF & bArray[i]);
                }
            }
        }
        
	return "HASHED" + hash;
    }

    public String generateToken() {
        return UUID.randomUUID().toString();
    }
    
    public boolean isTokenValid(User user, String token) {
        Date now = new Date();
        
        if (user.getToken().equals(token) && now.after(user.getTokenExpiration()))
            return true;
        
        return false;
    }
    
    public User getUserByToken(String token) {
        User user = null;
        
        try {
            user = userDao.getByToken(token);
        }
        catch (Exception e) {
            // No user;
            return null;
        }
        
        Date now = new Date();
        
        if (user.getToken().equals(token) && now.after(user.getTokenExpiration()))
            return user;
        
        return null;
    }
}
